package com.karl.dao.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.swing.plaf.synth.SynthSpinnerUI;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.karl.dao.UserDao;
import com.karl.entity.User;
@Component("userDao")
public class UserDaoImpl implements UserDao {

	private HibernateTemplate hibernateTemplate;
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}


	@Override
	public void save(User u) {
		System.out.println("hibernate + " + u.getUsername() + " " + u.getPassword());
		hibernateTemplate.save(u);
	}

	@Override
	public boolean checkUserExistsWithName(String username) {
		List<User> users = hibernateTemplate.find("from User u where u.username = '"+username+"'");
		
		int size = users.size();
		
		if(users != null && size > 0)
			return true;
		return false;
	}
	@Override
	public List<User> getUsers() {
		// TODO Auto-generated method stub
		return (List<User>)this.hibernateTemplate.find("from User");
	}
	@Override
	public User loadUserById(int id) {
		// TODO Auto-generated method stub
		return this.hibernateTemplate.load(User.class, id);
//		return this.hibernateTemplate.get(User.class, id);
	}

}
